package com.intelligent.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.dto.PurchaseStorageAddDTO;
import com.intelligent.dto.PurchaseStorageQueryDTO;
import com.intelligent.service.PurchaseStorageService;
import com.intelligent.utils.Result;
import com.intelligent.vo.PurchaseStorageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 采购入库控制器
 */
@RestController
@RequestMapping("/purchase/storage")
public class PurchaseStorageController {

    @Autowired
    private PurchaseStorageService purchaseStorageService;

    /**
     * 分页查询采购入库列表
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @PostMapping("/page")
    public Result<Page<PurchaseStorageVO>> getPurchaseStoragePage(@RequestBody PurchaseStorageQueryDTO queryDTO) {
        try {
            Page<PurchaseStorageVO> page = purchaseStorageService.getPurchaseStoragePage(queryDTO);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 新增采购入库
     * @param addDTO 新增数据
     * @return 是否成功
     */
    @PostMapping
    public Result<Boolean> addPurchaseStorage(@RequestBody PurchaseStorageAddDTO addDTO) {
        try {
            boolean success = purchaseStorageService.addPurchaseStorage(addDTO);
            return success ? Result.success(true) : Result.error("新增采购入库失败");
        } catch (Exception e) {
            return Result.error("新增失败：" + e.getMessage());
        }
    }

    /**
     * 获取采购入库详情
     * @param id 采购入库ID
     * @return 采购入库详情
     */
    @PostMapping("/getDetail")
    public Result<PurchaseStorageVO> getPurchaseStorageDetail(@RequestParam("id") Long id) {
        try {
            PurchaseStorageVO detail = purchaseStorageService.getPurchaseStorageDetail(id);
            if (detail == null) {
                return Result.error("采购入库记录不存在");
            }
            return Result.success(detail);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 修改采购入库
     * @param id 采购入库ID
     * @param addDTO 修改数据
     * @return 是否成功
     */
    @PutMapping("/{id}")
    public Result<Boolean> updatePurchaseStorage(@PathVariable Long id, @RequestBody PurchaseStorageAddDTO addDTO) {
        try {
            boolean success = purchaseStorageService.updatePurchaseStorage(id, addDTO);
            return success ? Result.success(true) : Result.error("修改采购入库失败");
        } catch (Exception e) {
            return Result.error("修改失败：" + e.getMessage());
        }
    }

    /**
     * 删除采购入库
     * @param ids 采购入库ID列表
     * @return 是否成功
     */
    @DeleteMapping("/{ids}")
    public Result<Boolean> deletePurchaseStorage(@PathVariable List<Long> ids) {
        try {
            boolean success = purchaseStorageService.removeByIds(ids);
            return success ? Result.success(true) : Result.error("删除采购入库失败");
        } catch (Exception e) {
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 导出采购入库
     * @param queryDTO 查询条件
     * @return 导出结果
     */
    @PostMapping("/export")
    public Result<String> exportPurchaseStorage(@RequestBody PurchaseStorageQueryDTO queryDTO) {
        try {
            // 这里可以实现导出功能，暂时返回成功
            return Result.success("导出功能待实现");
        } catch (Exception e) {
            return Result.error("导出失败：" + e.getMessage());
        }
    }
}
